namespace FileCollection
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    /// <summary>
    ///  IO Helper
    /// </summary>
    /// <Author>Alan Santos Rodrigues</Author>
    /// <Date>17/05/2007</Date>
    public static class IOHelper
    {
        public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
        {
            string[] filters = searchPattern.Split(';');
            List<string> files =
                new List<string>();

            foreach (string filter in filters)
            {
                files.AddRange(Directory.GetFiles(path, filter, searchOption));
            }

            return files.ToArray();
        }

        public static string[] GetFiles(string path, string searchPattern)
        {
            return GetFiles(path, searchPattern, SearchOption.AllDirectories);
        }

        public static FileInfo[] GetFileInfos(string path, string searchPattern, SearchOption searchOption, FileOrder fileOrder)
        {
            string[] filters = searchPattern.Split(';');
            List<FileInfo> files = new List<FileInfo>();

            DirectoryInfo directory = new DirectoryInfo(path);

            foreach (string filter in filters)
            {
                files.AddRange(directory.GetFiles(filter, searchOption));
            }

            FileInfo[] fileInfos = files.ToArray();

            switch (fileOrder)
            {
                case FileOrder.None:
                    return fileInfos;
                case FileOrder.CreationTime:
                    Array.Sort<FileInfo>(fileInfos, new CompareFileInfoByCreationTime());
                    return fileInfos;
                case FileOrder.Name:
                    Array.Sort<FileInfo>(fileInfos, new CompareFileInfoByName());
                    return fileInfos;
                case FileOrder.LastAccessTime:
                    Array.Sort<FileInfo>(fileInfos, new CompareFileInfoByLastAccessTime());
                    return fileInfos;
                case FileOrder.LastWriteTime:
                    Array.Sort<FileInfo>(fileInfos, new CompareFileInfoByLastWriteTime());
                    return fileInfos;
                case FileOrder.Length:
                    Array.Sort<FileInfo>(fileInfos, new CompareFileInfoByLength());
                    return fileInfos;
                default:
                    return fileInfos;
            }
        }

        public static FileInfo[] GetFileInfos(string path, string searchPattern, SearchOption searchOption)
        {
            return GetFileInfos(path, searchPattern, searchOption, FileOrder.None);
        }

        public static FileInfo[] GetFileInfos(string path, string searchPattern)
        {
            return GetFileInfos(path, searchPattern, SearchOption.AllDirectories, FileOrder.None);
        }
    }
}
